﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace vorkos_dev_snake
{
    [Serializable]
    public class ConsoleUI : IUserInterface
    {
        private const string APPLE = "@";
        private const string WALL = "#";
        private const string ERASE = " ";
        private const string PAUSE = "Pause... ";
        private const string GAMEOVER = "Game Over.";

        public event EventArgsMoved KeyPressed;
        private void OnKeyPressed(Snake snake, Movement pressedKey)
        {
            if (KeyPressed != null) KeyPressed(snake, pressedKey);
        }

        public Boolean ReadKey(Snake snake)
        {
            ConsoleKeyInfo pressedKey = new ConsoleKeyInfo();
            if (Console.KeyAvailable) pressedKey = Console.ReadKey(true);
            else OnKeyPressed(snake, Movement.None);

            if (pressedKey.Key == ConsoleKey.DownArrow) OnKeyPressed(snake, Movement.Down);
            else if (pressedKey.Key == ConsoleKey.UpArrow) OnKeyPressed(snake, Movement.Up);
            else if (pressedKey.Key == ConsoleKey.LeftArrow) OnKeyPressed(snake, Movement.Left);
            else if (pressedKey.Key == ConsoleKey.RightArrow) OnKeyPressed(snake, Movement.Right);
            else if (pressedKey.Key == ConsoleKey.P) Menu.Instance.CallMenu(snake, snake.CurrentField, GameLogic.Instance);
            else if (pressedKey.Key == ConsoleKey.Escape) GameLogic.Instance.isGameOver = true;
            else OnKeyPressed(snake, Movement.None);
            return true;
        }
        public Coordinate GetDimmentions()
        {
            Coordinate dimmentions = new Coordinate();
            dimmentions.X = (ushort)(Console.WindowWidth);
            dimmentions.Y = (ushort)(Console.WindowHeight);
            return dimmentions;
        }
        public Coordinate GetCenterPoint()
        {
            Coordinate center = new Coordinate();
            center.X = (ushort)(Console.WindowWidth / 2);
            center.Y = (ushort)(Console.WindowHeight / 2);
            return center;
        }
        public void DrawField(Field curField)
        {
            Coordinate[] drawField = curField.GetField();
            Console.CursorVisible = false;
            foreach (Coordinate item in drawField)
            {
                Console.SetCursorPosition(item.X, item.Y);
                Console.Write(WALL);
            }
        }
        public void DrawFood(Field curField)
        {
            Coordinate[] drawFood = curField.GetFood().ToArray();
            foreach (Coordinate item in drawFood)
            {
                Console.SetCursorPosition(item.X, item.Y);
                Console.Write(APPLE);
            }
        }
        public void DrawSnake(Snake snake, Movement move)
        {
            Coordinate head = ((Snake)snake).GetSnakeCoordinates().Last();
            Coordinate tail = ((Snake)snake).GetSnakeCoordinates().First();
            Console.SetCursorPosition(head.X, head.Y);
            Console.Write(WALL);
            Console.SetCursorPosition(tail.X, tail.Y);
            Console.Write(ERASE);
        }
        public void GameOver()
        {
            Console.Clear();
            Console.SetCursorPosition(20, 10);
            Console.WriteLine(GAMEOVER);
        }
        public void DrawMenu()
        {
            Console.Clear();
            Console.WriteLine(PAUSE);
            Thread.Sleep(1000);
        }
    }

    public enum Movement
    {
        Up = 0,
        Down = 1,
        Left = 2,
        Right = 4,
        None
    }
}
